package endpoint

import (
	"context"
	"gowork/memberGoKit/model"
	"github.com/go-kit/kit/endpoint"
	"gowork/memberGoKit/server/service"
	"encoding/json"
	"errors"
)

type MemberNilRequest struct{

}

type MemberRequestOrResponse struct{
	MemberList string  `json:"memberList"`
}

type MemberMapResponse struct{
	Message string `json:"message"`
	Code string `json:"code"`
}

func MemberInsertEnpoint(ssc service.MemberSriCuda) endpoint.Endpoint{
	return func(ctx context.Context, request interface{}) (response interface{}, err error){
		r := request.(MemberRequestOrResponse)
		
		var mmm []*model.Member

		if uerr := json.Unmarshal([]byte(r.MemberList), &mmm); uerr == nil{
			result := ssc.InesrtSri(mmm)
			if result["code"] == "200"{
				return MemberMapResponse{Message: result["message"], Code : result["code"]}, nil
			}else{
				return nil, errors.New("插入失败")
			}
		}else{
			return MemberMapResponse{Message: "解析失败", Code : "400"}, nil
		}
	}
}

func MemberQueryEnpoint(ssc service.MemberSriCuda) endpoint.Endpoint{
	return func(ctx context.Context, request interface{}) (response interface{}, err error){
		receive := ssc.QuerySri()
		if result, err := json.Marshal(receive); err == nil{
			return MemberRequestOrResponse{MemberList: string(result)}, nil
		}else{
			return nil, errors.New("编码失败")
		}
		
	}
}